using System;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Controller;
using FilteredStatistics.Data;

namespace FilteredStatistics.View
{
    partial class SaveTemplateDialog : Form
    {
        public SaveTemplateDialog(FilterList filters)
        {
            InitializeComponent();

            this.Text = PluginView.GetLocalizedString("SaveTemplateText");
            SaveAsNewRadioButton.Text = PluginView.GetLocalizedString("SaveAsNewRadioButtonText");
            UpdateExistingRadioButton.Text = PluginView.GetLocalizedString("UpdateExistingRadioButtonText");
            TemplateNameTextBox.Text = PluginView.GetLocalizedString("NewTemplateText");
            OKButton.Text = CommonResources.Text.ActionOk;
            Cancel_Button.Text = CommonResources.Text.ActionCancel;

            SaveAsNewRadioButton.Checked = true;
            UpdateExistingRadioButton.Checked = false;

            // Fill combo box with existing templates
            if (TemplateManager.Instance.GetTemplateCount() > 0)
            {
                for (int i = 0; i < TemplateManager.Instance.GetTemplateCount(); ++i)
                {
                    Template currentTemplate = TemplateManager.Instance.GetTemplate(i);

                    // Make sure we can't save and create a loop
                    if (!filters.ContainsTemplateLoop(currentTemplate))
                    {
                        TemplatesComboBox.Items.Add(currentTemplate.Name);
                    }
                }

                if (TemplatesComboBox.Items.Count > 0)
                {
                    TemplatesComboBox.SelectedIndex = 0;
                }
            }

            TemplateNameTextBox.ThemeChanged(PluginMain.GetApplication().VisualTheme);
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            bool close = true;
            if (SaveAsNew)
            {
                if (TemplateNameTextBox.Text == "")
                {
                    close = false;

                    MessageBox.Show(PluginView.GetLocalizedString("MustEnterNameText"),
                                    PluginView.GetLocalizedString("ErrorText"),
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (TemplateManager.Instance.GetTemplate(TemplateNameTextBox.Text) != null)
                {
                    DialogResult res;

                    res = MessageBox.Show(PluginView.GetLocalizedString("OverwriteExistingTemplateText"),
                                          PluginView.GetLocalizedString("WarningText"),
                                          MessageBoxButtons.YesNo,
                                          MessageBoxIcon.Question);

                    if (res == DialogResult.Yes)
                    {
                        SaveAsNewRadioButton.Checked = false;
                        UpdateExistingRadioButton.Checked = true;
                        TemplatesComboBox.SelectedItem = TemplateNameTextBox.Text;
                    }
                    else
                    {
                        close = false;
                    }
                }
            }

            if(close)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            TemplateNameTextBox.Enabled = SaveAsNewRadioButton.Checked;
            TemplatesComboBox.Enabled = UpdateExistingRadioButton.Checked &&
                                        TemplateManager.Instance.GetTemplateCount() > 0;

            if (UpdateExistingRadioButton.Checked)
            {
                OKButton.Enabled = TemplatesComboBox.Enabled;
            }
            else
            {
                OKButton.Enabled = TemplateNameTextBox.Text != "";
            }
        }

        private void TemplateNameTextBox_TextChanged(object sender, EventArgs e)
        {
            if (SaveAsNewRadioButton.Checked)
            {
                OKButton.Enabled = TemplateNameTextBox.Text != "";
            }
        }

        public bool SaveAsNew
        {
            get { return SaveAsNewRadioButton.Checked; }
        }

        public String TemplateName
        {
            get
            {
                if (SaveAsNew)
                {
                    return TemplateNameTextBox.Text;
                }
                else
                {
                    return TemplatesComboBox.SelectedItem as String;
                }
            }
        }
    }
}